package cn.iocoder.yudao.module.self.controller.admin.student;

import cn.iocoder.yudao.module.self.mq.service.MyRabbitMqConsumerService;
import cn.iocoder.yudao.module.self.mq.service.MyRabbitMqProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.self.controller.admin.student.vo.*;
import cn.iocoder.yudao.module.self.dal.dataobject.student.StudentDO;
import cn.iocoder.yudao.module.self.service.student.StudentService;

@Tag(name = "管理后台 - 学生表--测试使用")
@RestController
@RequestMapping("/self/student")
@Validated
public class StudentController {

    @Resource
    private StudentService studentService;

    @Autowired
    MyRabbitMqProductService myRabbitMqProductService;



    @Autowired
    MyRabbitMqConsumerService myRabbitMqConsumerService;

    @PostMapping("/create")
    @Operation(summary = "创建学生表--测试使用")
    @PreAuthorize("@ss.hasPermission('self:student:create')")
    public CommonResult<String> createStudent(@Valid @RequestBody StudentSaveReqVO createReqVO) {
//        myRabbitMqProductService.sendDirectMessage(createReqVO);
        myRabbitMqProductService.sendTopicMessage1(createReqVO);
        myRabbitMqProductService.sendTopicMessage2(createReqVO);
        myRabbitMqProductService.sendTopicMessage3(createReqVO);
        myRabbitMqProductService.sendTopicMessage4(createReqVO);
        return success("ok");
//        return success(studentService.createStudent(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新学生表--测试使用")
    @PreAuthorize("@ss.hasPermission('self:student:update')")
    public CommonResult<Boolean> updateStudent(@Valid @RequestBody StudentSaveReqVO updateReqVO) {
//        studentService.updateStudent(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除学生表--测试使用")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('self:student:delete')")
    public CommonResult<Boolean> deleteStudent(@RequestParam("id") String id) {
//        studentService.deleteStudent(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得学生表--测试使用")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('self:student:query')")
    public CommonResult<StudentRespVO> getStudent(@RequestParam("id") String id) {
        StudentDO student = studentService.getStudent(id);
        return success(BeanUtils.toBean(student, StudentRespVO.class));
//        return success(StudentRespVO.class);
    }

    @GetMapping("/page")
    @Operation(summary = "获得学生表--测试使用分页")
    @PreAuthorize("@ss.hasPermission('self:student:query')")
    public CommonResult<PageResult<StudentRespVO>> getStudentPage(@Valid StudentPageReqVO pageReqVO) {
//        myRabbitMqConsumerService.consumeMessage();
//        myRabbitMqConsumerService.consumeTopicMessage2();
//        PageResult<StudentDO> pageResult = studentService.getStudentPage(pageReqVO);

//        return success(BeanUtils.toBean(pageResult, StudentRespVO.class));
        return null;
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出学生表--测试使用 Excel")
    @PreAuthorize("@ss.hasPermission('self:student:export')")
    @OperateLog(type = EXPORT)
    public void exportStudentExcel(@Valid StudentPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StudentDO> list = studentService.getStudentPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "学生表--测试使用.xls", "数据", StudentRespVO.class,
                        BeanUtils.toBean(list, StudentRespVO.class));
    }

}